import { message as antdMessage, Modal, notification } from "antd";
import { MessageInstance } from "antd/es/message/interface";
import { HookAPI } from "antd/es/modal/useModal";
import { NotificationInstance } from "antd/es/notification/interface";
import { useEffect } from "react";

type ModalType = typeof Modal;
class GAction {
  public message: MessageInstance = antdMessage;
  public modal: HookAPI | ModalType = Modal;
  public notification: NotificationInstance | null = null;

  public setAntDGlobalApi(messageApi: any, modalApi: any, notification: any) {
    this.message = messageApi;
    this.modal = modalApi;
    this.notification = notification;
  }
}

const GFS = new GAction();
export { GFS };//Global Feedback System
export function getGlobalAntDActionContext() {
  const [messageApi, messageContextHolder] = antdMessage.useMessage();
  const [modalApi, modalContextHolder] = Modal.useModal();
  const [notificationApi, notificationContextHolder] = notification.useNotification();
  useEffect(() => {
    GFS.setAntDGlobalApi(messageApi, modalApi,notificationApi);
    return () => {
      GFS.setAntDGlobalApi(null, null, null);
    };
  }, []);

  return (
    <>
      {messageContextHolder}
      {modalContextHolder}
      {notificationContextHolder}
    </>
  );
}
